package com.fdb.efp.cus.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.fdb.efp.cus.service.dao.BlackListInfoDao;
import com.fdb.efp.cus.service.domain.BlackListInfo;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.cus.service.facade.BlackListInfoService;
import com.fdb.efp.cus.service.vo.BlackListInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>ServiceImpl: 黑名单信息表				<p>
 * @author ligm
 * @date 2019-08-10
 */
@Service("blackListInfoService")
public class BlackListInfoServiceImpl implements BlackListInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BlackListInfoServiceImpl.class);

	@Autowired
	private BlackListInfoDao blackListInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertBlackListInfo(BlackListInfoVO inBlackListInfoVo) {
		logger.debug("当前新增数据为:"+ inBlackListInfoVo.toString());
		int num = 0;
		try {
			BlackListInfo blackListInfo = new BlackListInfo();
			beanCopy(inBlackListInfoVo, blackListInfo);
			num = blackListInfoDao.insertBlackListInfo(blackListInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BlackListInfoVO inBlackListInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBlackListInfoVo);
		int num = 0;
		try {
			BlackListInfo blackListInfo = new BlackListInfo();
			beanCopy(inBlackListInfoVo, blackListInfo);
			num = blackListInfoDao.deleteByPk(blackListInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBlackListInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BlackListInfoVO inBlackListInfoVo) {
		logger.debug("当前修改数据为:"+ inBlackListInfoVo.toString());
		int num = 0;
		try {
			BlackListInfo blackListInfo = new BlackListInfo();
			beanCopy(inBlackListInfoVo, blackListInfo);
			num = blackListInfoDao.updateByPk(blackListInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBlackListInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BlackListInfoVO queryByPk(BlackListInfoVO inBlackListInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inBlackListInfoVo);
		try {
			BlackListInfo queryBlackListInfo = new BlackListInfo();
			beanCopy(inBlackListInfoVo, queryBlackListInfo);
			BlackListInfo queryRslBlackListInfo = blackListInfoDao.queryByPk(queryBlackListInfo);
			if (Objects.nonNull(queryRslBlackListInfo)) {
				BlackListInfoVO outBlackListInfoVo = beanCopy(queryRslBlackListInfo, new BlackListInfoVO());
				logger.debug("当前查询结果为:"+ outBlackListInfoVo.toString());
				return outBlackListInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}




	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BlackListInfoVO> queryAllOwner(BlackListInfoVO blackListInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BlackListInfoVO> list = null;
		try {
			List<BlackListInfo> blackListInfos = blackListInfoDao.queryAllOwnerByPage(blackListInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ blackListInfos.size());
			pageSet(blackListInfos, blackListInfoVo);
			list = (List<BlackListInfoVO>) beansCopy(blackListInfos, BlackListInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BlackListInfoVO> queryAllCurrOrg(BlackListInfoVO blackListInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BlackListInfo> blackListInfos = blackListInfoDao.queryAllCurrOrgByPage(blackListInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+blackListInfos.size());
		List<BlackListInfoVO> list = null;
		try {
			pageSet(blackListInfos, blackListInfoVo);
			list = (List<BlackListInfoVO>) beansCopy(blackListInfos, BlackListInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BlackListInfoVO> queryAllCurrDownOrg(BlackListInfoVO blackListInfoVo) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BlackListInfo> blackListInfos = blackListInfoDao.queryAllCurrDownOrgByPage(blackListInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ blackListInfos.size());
		List<BlackListInfoVO> list = null;
		try {
			pageSet(blackListInfos, blackListInfoVo);
			list = (List<BlackListInfoVO>) beansCopy(blackListInfos, BlackListInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@Override
	public int insertBlackListInfoExp(BlackListInfoVO blackListInfoVO) throws Exception {
		logger.debug("当前新增数据为：" + JSON.toJSONString(blackListInfoVO));
		BlackListInfo blackListInfo = new BlackListInfo();
		int num = 0;
		try {
			beanCopy(blackListInfoVO, blackListInfo);
			num = blackListInfoDao.insertBlackListInfo(blackListInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为： "+ num);
		return num;
	}

	/**
	 * @description:批量插入黑名单信息表
	 * @param blackListInfoVo
	 * @return

	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertBlackListInfoBatch(List<BlackListInfoVO> inserBlackListInfo) {
		logger.debug("当前新增数据条数为:"+ inserBlackListInfo.size());
		int num = 0;
		try {
			num = blackListInfoDao.insertBlackListInfoBatch(inserBlackListInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		return num;
	}

	@Override
	public int batchDeleteByPk(BlackListInfoVO inBlackListInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBlackListInfoVo);
		int num = 0;
		try {
			List<String> blackInfoList = null;
			if (Objects.nonNull(inBlackListInfoVo) && !StringUtil.isNullorBank(inBlackListInfoVo.getCertCode())) {
				blackInfoList = Arrays.asList(inBlackListInfoVo.getCertCode().split(","));
			}

			num = blackListInfoDao.batchDeleteByPk(blackInfoList);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBlackListInfoVo +"删除的数据条数为"+ num);
		return num;
	}

    @Override
    public List<BlackListInfoVO> queryByCertCodeAndType(BlackListInfoVO blackListInfoVo) {
		logger.debug("当前查询参数信息为:"+ blackListInfoVo);
		List<BlackListInfoVO> list = null;
		try {
			BlackListInfo queryBlackListInfo = new BlackListInfo();
			beanCopy(blackListInfoVo, queryBlackListInfo);
			List<BlackListInfo> queryRslBlackListInfo = blackListInfoDao.queryByCertCodeAndType(queryBlackListInfo);
			if (Objects.nonNull(queryRslBlackListInfo)&&!queryRslBlackListInfo.isEmpty()) {
				List<BlackListInfoVO> outBlackListInfoVo = (List<BlackListInfoVO>) beansCopy(queryRslBlackListInfo, BlackListInfoVO.class);
				logger.debug("当前查询结果为:"+ outBlackListInfoVo.toString());
				return outBlackListInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
    }


}
